﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XWord.Fundamental.Domain;
using XWord.GUI.Views;
using mshtml;
using System.Runtime.InteropServices;
using System.Windows.Controls.Primitives;
using XWord.WPF.ApplicationFramework;

namespace XWord.GUI.Views.Words
{
    /// <summary>
    /// Interaction logic for DictOnlineWordExplainView.xaml
    /// </summary>
    public partial class DictOnlineWordExplainView : UserControl
    {
        public DictOnlineWordExplainView()
        {
            InitializeComponent();
           
        }

        private void WebBrowser_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var brower = sender as WebBrowser;
            string word=string.Empty;
            if (e.NewValue == null && e.OldValue != null)
            {
                word = (string)e.OldValue;
            }
            else
                word = (string)e.NewValue;
           
            if (string.IsNullOrEmpty(word)) return;
            
            brower.Source = new Uri(string.Format("http://dict.cn/mini.php?q={0}", word));
        }

        private void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            var element=(sender as WebBrowser).Document;
            mshtml.HTMLDocument doc = element as HTMLDocument;
            if (doc != null)
            {
                DHTMLEventHandler handler = new DHTMLEventHandler(doc);
                handler.Handler += new DHTMLEvent(this.DictOnlineWordExplainView_oncontextmenu);
                ((DispHTMLDocument)doc).oncontextmenu = handler;
            }
        }

        void DictOnlineWordExplainView_oncontextmenu(IHTMLEventObj pEvtObj)
        {
            webBrower.ContextMenu.Placement = PlacementMode.Mouse;
            webBrower.ContextMenu.IsOpen = true;
            pEvtObj.returnValue = false;
          
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var txtRange = (webBrower.Document as HTMLDocument).selection.createRange() as IHTMLTxtRange;
            
            Clipboard.SetText(txtRange.text);
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var txtRange = (webBrower.Document as HTMLDocument).selection.createRange() as IHTMLTxtRange;
            DefaultReadWordEngine.UseTTSReadWord(txtRange.text);
        }
    }

    public delegate void DHTMLEvent(IHTMLEventObj e);

    [ComVisible(true)]
    public class DHTMLEventHandler
    {
        public DHTMLEvent Handler;
        HTMLDocument Document;

        public DHTMLEventHandler(HTMLDocument doc)
        {
            this.Document = doc;
        }
        [DispId(0)]
        public void Call()
        {
            Handler(Document.parentWindow.@event);
        }
    }
}
